package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Goods;
import com.example.demo.entity.ResponseResult;
import com.example.demo.mapper.GoodsMapper;
import com.example.demo.service.GoodsService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    /**
     * 这里就是返回全部的商品信息
     * @return
     */
    @Override
    public ResponseResult getList() {
        List<Goods> goods = list();

        if(goods.isEmpty()){
            return ResponseResult.ok("超市中没有商品");
        }

        return ResponseResult.ok(goods);
    }

    @Override
    public ResponseResult getByName(String name) {
        List<Goods> goods = list(new QueryWrapper<Goods>().eq("Goods_name", name));
        if(goods.isEmpty()){
            return ResponseResult.ok("没有对应的商品");
        }
        return ResponseResult.ok(goods);
    }

    /**
     * 新增商品信息
     * @param goods
     * @return
     */
    @Override
    public ResponseResult savaGoods(Goods goods) {
        boolean isSuccess = save(goods);
        if(!isSuccess){
            return ResponseResult.fail("新增失败");
        }
        return ResponseResult.ok("新增成功");
    }

    @Override
    public ResponseResult updateGoods(Goods goods) {
        if(goods == null)
            return ResponseResult.fail("请输入正确的商品信息");
        boolean isSuccess = update(goods, new QueryWrapper<Goods>().eq("Goods_id", goods.getId()));

        if(!isSuccess){
            return ResponseResult.fail("修改失败");
        }

        return ResponseResult.ok("修改成功");
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(String id) {
        boolean isSuccess = remove(new QueryWrapper<Goods>().eq("Goods_id", id));
        if(!isSuccess){
            return ResponseResult.fail("删除失败");
        }
        return ResponseResult.ok("删除成功");
    }

    /**
     * 根据名称删除
     * @param name
     * @return
     */
    @Override
    public ResponseResult deleteByName(String name) {
        boolean isSuccess = remove(new QueryWrapper<Goods>().eq("Goods_name", name));
        if(!isSuccess){
            return ResponseResult.fail("删除失败");
        }
        return ResponseResult.ok("删除成功");
    }
}
